package dbms.ypr;

import dbms.ypr.user.DataBasePermissions;
import dbms.ypr.user.TablePermissions;
import dbms.ypr.user.User;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class Show {

    public static void showSql(String sql){

        //列出所有数据库
        boolean b = sql.endsWith(" databases;");
        if(b){
            String path = dbms.ypr.SQLConstant.getPath();
            List<String> dbList = dbms.ypr.Utils.getAllDatabase(path);
            List<String> db = new ArrayList<>();
            db.add("Database");
            List<List<String>> list = new ArrayList<>();
            User loginUser = User.getLoginUser(dbms.ypr.SQLConstant.currentUsername);
            List<String> collect = null;
            if (loginUser.getDataBasePermissions() != null){
                collect = loginUser.getDataBasePermissions().stream().map(DataBasePermissions::getDatabaseName).collect(Collectors.toList());
            } else {
                collect = new ArrayList<>();
            }
            for(int i = 0; i < dbList.size(); i++){
                List<String> ls = new ArrayList<>();
                String value = dbList.get(i);
                if (!collect.contains(value)) continue;
                ls.add(value);
                list.add(ls);
            }
            System.out.println(dbms.ypr.TableGenerator.generateTable(db, list));
            Input.get();
        }

        //列出所有该数据库下所有的表格
        else{
            boolean c = sql.endsWith(" tables;");
            if(c){
                String nowPath = dbms.ypr.SQLConstant.getNowPath();
                List<String> tableList = dbms.ypr.Utils.getAllTables(nowPath);
                List<String> list = new ArrayList<>();

                //获取数据库名
                int index = nowPath.lastIndexOf("\\");
//                System.out.println(nowPath);
//                System.out.println(index);
//                System.out.println(nowPath.length());
                String dbName = nowPath.substring(index+1, nowPath.length());

                list.add(dbName);
                User loginUser = User.getLoginUser(dbms.ypr.SQLConstant.currentUsername);
                List<String> collect = null;
                if (loginUser.getTablePermissions() != null){
                    collect = loginUser.getTablePermissions().stream().map(TablePermissions::getTableName).collect(Collectors.toList());
                } else {
                    collect = new ArrayList<>();
                }
                List<List<String>> tList = new ArrayList<>();
                for(String t:tableList){
                    List<String> list1 = new ArrayList<>();
                    if (!collect.contains(t)) continue;
                    list1.add(t);
                    tList.add(list1);
                }

                System.out.println(dbms.ypr.TableGenerator.generateTable(list, tList));
                Input.get();
            }
        }
        System.out.println("ERROR: show语句执行错误");
        Input.get();
    }


}
